package cn.nexd.app.collector.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import java.util.ArrayList;
import java.util.List;

import cn.nexd.app.collector.R;
import cn.nexd.app.collector.adapter.CollectionListAdapter;
import cn.nexd.app.collector.model.ErrorLog;
import cn.nexd.app.collector.presenter.fileManager.CollectFileManagerPresenter;
import cn.nexd.app.collector.presenter.fileManager.FileManagerContract;

/**
 * Created by xunzongxia on 16/10/9.
 */

public class CollectFileFragment extends Fragment implements FileManagerContract.View {

    private ExpandableListView collectList;

    private List<ErrorLog> errorLogs;
    private CollectionListAdapter collectionListAdapter;

    private FileManagerContract.Presenter presenter;

    public static CollectFileFragment newInstance(int position) {

        Bundle args = new Bundle();

        CollectFileFragment fragment = new CollectFileFragment();
        args.putInt("position", position);
        fragment.setArguments(args);
        return fragment;
    }

    //测试用的假数据
    private void initData() {
        createErrorLog();
    }

    private void createErrorLog() {
        errorLogs = new ArrayList<>();
        presenter.loadFile();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        presenter = new CollectFileManagerPresenter(this.getActivity(), this);
        initData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_collect_info, container, false);
        collectList = (ExpandableListView) layout.findViewById(R.id.collect_file_list);
        bindAdapter();
        bindItemClick();
        return layout;
    }

    private void bindAdapter() {
        collectList.setGroupIndicator(null);
        collectionListAdapter = new CollectionListAdapter(getContext(), errorLogs);
        collectList.setAdapter(collectionListAdapter);
    }

    private void bindItemClick() {
        collectList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                errorLogs.get(groupPosition).setFileName(presenter.loadChildFilesName(errorLogs.get(groupPosition).getDate()));
                return false;
            }
        });

        collectList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                for (int i = 0; i < collectionListAdapter.getGroupCount(); i++) {
                    if (groupPosition != i) {
                        collectList.collapseGroup(i);
                    }
                }
            }
        });
    }

    @Override
    public void updateList(List<ErrorLog> logs) {
        errorLogs.addAll(logs);
    }

    @Override
    public void initView() {

    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void startActivity() {

    }

    @Override
    public void printLog(String msg) {

    }
}
